import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from "../components/Home";
import store from "../store/store";

Vue.use(VueRouter)

  const routes = [
    {
      path: '/home',
      name: '首页信息',
      component: Home,
      meta: {
        requireAuth: true,
      }
    },
    {
      path: '/article/:articleId',
      name: '博文编辑',
      component: ()=>import("../components/Article"),
      meta: {
        requireAuth: true,
      }
    },
    {
      path: '/article',
      redirect: '/article/-1'
    },
    {
      path: '/management',
      name: '应用管理',
      component: ()=>import("../components/Management"),
      meta: {
        requireAuth: true,
      }
    },
    {
      path: '/login',
      name: '登录',
      component: ()=>import("../components/Login")
    },
    {
      path: '/',
      redirect: '/home'
    }

]

const router = new VueRouter({
  mode: 'history',
  base: "/",
  routes
})

router.beforeEach((to,from,next)=>{
  // 登录检测
  if(to.meta.requireAuth == true){
    if(store.state.token.flag === true){
      next()
    }else{
      next('/login');
    }
  }else{  //不需要登录权限
    next();
  }
})

// 解决重复跳转抛异常问题
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
};

export default router
